package org.pms.demo.webservice.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.pms.demo.webservice.exception.IMReadException;
import org.pms.demo.webservice.service.ResourceVersionService;
import org.pms.demo.webservice.utils.ProjectUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

/**
 * Created by xusaike on 17/6/27.
 */
@Component
@Path("v{version}/resource")
public class ResourceVersionRestController {
    @Resource
    private ResourceVersionService resourceVersionService;

    @GET
    @Path("list")
    @RequiresPermissions("resource:view")
    public String getResourceList(){

        try{
            Subject currentUser = SecurityUtils.getSubject();//获取当前用户
            System.out.println();
            return resourceVersionService.getAllResoureList();
        }catch (Exception e){
            IMReadException.printf(e);
        }
        return ProjectUtils.getErrorMsg("error");
    }
    @POST
    @Path("add")
    @RequiresPermissions("resource:create")
    public String addResource(@FormParam("pid")int pid,@FormParam("name")String name,@FormParam("permission")String permission,@FormParam("node_type")String node_type,@FormParam("url")String url,@FormParam("route")String route){
        try{
            return resourceVersionService.addResource(pid,name,permission,node_type,url,route);
        }catch (Exception e){
            IMReadException.printf(e);
        }
        return ProjectUtils.getErrorMsg("error");
    }
    @POST
    @Path("delete")
    @RequiresPermissions("resource:delete")
    public String deleteResource(@FormParam("id")int id){
        try{
            String rt = resourceVersionService.deleteResource(id);
            return rt;
        }catch (Exception e){
            IMReadException.printf(e);
        }
        return ProjectUtils.getErrorMsg("error");
    }
    @POST
    @Path("update")
    @RequiresPermissions("resource:update")
    public String updateResource(@FormParam("id")int id,@FormParam("name")String name,@FormParam("permission")String permission,@FormParam("node_type")String node_type,@FormParam("url")String url,@FormParam("route")String route,@FormParam("available")int available,@FormParam("type")int type){
        try{
            return resourceVersionService.updateResource(id,name,permission,node_type,url,route,available,type);
        }catch (Exception e){
            IMReadException.printf(e);
        }
        return ProjectUtils.getErrorMsg("error");
    }
}
